// pages/aftersalebox/aftersalecreate/aftersalecreate.js
import {
  getaftersaleinitinfo,
  aftersaleCreate
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataInfo: null,
    personInfo: null,
    type_id: "",
    proname: "",
    afaddress:"",
    afmemo:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    getaftersaleinitinfo((r) => {
      if (r.code == 0) {
        self.setData({
          dataInfo: r.data
        })
      }
    })

  },
  //选择售后处理人
  goCheckAFPerson: function() {
    let self = this;

    wx.navigateTo({
      url: '../selectaftersaleperson/selectaftersaleperson?persons=' + escape(JSON.stringify(self.data.dataInfo.afconfigs)),
    })
  },
  //选择售后类型
  onchekAFType: function(e) {
    let self = this;
    let type_id = ""
    let item = e.currentTarget.dataset.item;
    self.data.dataInfo.aftypes.map((o, i) => {
      if (o.id == item.id) {
        o.selected = true;
        type_id = o.id;
      } else {
        o.selected = false;
      }
    })

    self.setData({
      type_id: type_id,
      dataInfo: self.data.dataInfo
    })
  },
  //获取商品名称
  getProName: function(e) {
    this.setData({
      proname: e.detail.value
    })
  },
  //获取售后地址
  getafaddress: function(e) {
    this.setData({
      afaddress: e.detail.value
    })
  },
  //获取售后备注
  getafmemo: function(e) {
    this.setData({
      afmemo: e.detail.value
    })
  }, 
  //创建售后
  aftersaleCreate: function() {
    let {
      proname,
      type_id,
      personInfo,
      afaddress,
      afmemo
    } = this.data;
    if (proname == "") {
      errorMsg("请输入商品名称")
    } else if (!personInfo) {
      errorMsg("请选择售后处理人")
    } else if (type_id == "") {
      errorMsg("请选择售后类型")
    } else {
      aftersaleCreate(proname, type_id, personInfo.id, afaddress,
        afmemo,(r) => {
        if (r.code == 0) {
          wx.navigateTo({
            url: '../aftersalecreatesuccess/aftersalecreatesuccess?id=' + r.data.id,
          })
        } else {
          errorMsg(r.message);
        }
      })
    }

  }

})